﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Net.Http;

namespace BW.Mes.WinForms.BasicData
{
    public partial class AddBomForm : DevExpress.XtraEditors.XtraForm
    {
        private readonly HttpClient _httpClient = new HttpClient();
        private readonly string _apiBaseUrl = "https://localhost:7274";

        public AddBomForm()
        {
            InitializeComponent();
            comboBoxEditType.Properties.Items.Clear();
            comboBoxEditType.Properties.Items.AddRange(new string[] { "原材料", "半成品", "成品", "其他" });
            comboBoxEditType.Properties.NullText = "请选择类型";
        }

        private async void btnOK_Click(object sender, EventArgs e)
        {
            var model = new
            {
                BomCode = textEditCode.Text,
                BomName = textEditName.Text,
                Version = textEditVersion.Text,
                BomType = comboBoxEditType.Text,
                BomNumber = textEditNumber.Text,
                BomMsg = textEditMsg.Text,
                CreatedBy = "yourUser"
            };
            try
            {
                string jsonData = JsonConvert.SerializeObject(model);
                var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
                _httpClient.DefaultRequestHeaders.Accept.Clear();
                _httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                var response = await _httpClient.PostAsync($"{_apiBaseUrl}/api/Bom/AddBom", content);
                response.EnsureSuccessStatusCode();
                XtraMessageBox.Show("新增成功！");
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"新增失败: {ex.Message}");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}